package com.ysd.lis.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ysd.common.Result;
import com.ysd.common.dto.DeleteDto;
import com.ysd.common.dto.DictionaryAddDto;
import com.ysd.lis.controller.pub.BaseController;
import com.ysd.lis.entity.TDictionary;
import com.ysd.lis.service.TDictionaryService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Peng
 * @since 2022-03-10
 */
@RestController
@RequestMapping("/api/sys")
public class TDictionaryController extends BaseController {
    @Autowired
    TDictionaryService tDictionaryService;
    @ApiOperation("获取参数列表的方法")
    @GetMapping("/findDictionaryList")
    public Result findDictionaryList(DictionaryAddDto dictionaryAddDto){
        Page<TDictionary> page = new Page<>();
        page.setSize(dictionaryAddDto.getPageSize());
        page.setCurrent(dictionaryAddDto.getPageIndex());
        LambdaQueryWrapper<TDictionary> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(dictionaryAddDto.getKey())){
            lambdaQueryWrapper.like(TDictionary::getDicKey,dictionaryAddDto.getKey());
        }
        if(StringUtils.isNotBlank(dictionaryAddDto.getName())){
            lambdaQueryWrapper.like(TDictionary::getDicName,dictionaryAddDto.getName());
        }
        if(StringUtils.isNotBlank(dictionaryAddDto.getTypeId())){
            lambdaQueryWrapper.eq(TDictionary::getTypeId,dictionaryAddDto.getTypeId());
        }
        if(StringUtils.isNotBlank(dictionaryAddDto.getOrgId())){
            lambdaQueryWrapper.eq(TDictionary::getOrgId,dictionaryAddDto.getOrgId());
        }
        Page<TDictionary> page1 = tDictionaryService.page(page,lambdaQueryWrapper);
        return Result.succ(1,"查询成功",page1);
    }

    @ApiOperation("添加参数时的校验")
    @GetMapping("/findDictionaryByKey")
    public Result findDictionaryByKey(@RequestParam(value = "orgId") String orgId,
                                      @RequestParam(value = "key") String dicKey){
        LambdaQueryWrapper<TDictionary> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(TDictionary::getOrgId,orgId).eq(TDictionary::getDicKey,dicKey);
        List<TDictionary> list = tDictionaryService.list(lambdaQueryWrapper);
        return Result.succ(1,"查询成功",list);
    }

    @ApiOperation("编辑")
    @PostMapping("/editDictionary")
    public Result editDictionary(@RequestBody TDictionary tDictionary){
        boolean b = tDictionaryService.updateById(tDictionary);
        if(b){
            return Result.succ(1,"修改成功","");
        }
        else{
            return Result.succ(400,"修改失败","");
        }
    }

    @ApiOperation("添加")
    @PostMapping("/addDictionary")
    public Result addDictionary(@RequestBody TDictionary tDictionary){
        boolean save = tDictionaryService.save(tDictionary);
        if(save){
            return Result.succ(1,"添加成功","");
        }
        else{
            return Result.succ(400,"添加失败","");
        }
    }

    @ApiOperation("删除")
    @PostMapping("/deleteDictionary")
    public Result deleteDictionary(@RequestBody DeleteDto deleteDto){
        boolean b = tDictionaryService.removeById(deleteDto.getId());
        if(b){
            return Result.succ(1,"删除成功","");
        }
        else{
            return Result.succ(400,"删除失败","");
        }
    }

}
